package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.Mobile;
import com.utility.MobileDAO;

/**
 * Servlet implementation class GetInsertServlet
 */
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int id = 0 ;
		ArrayList<String> validationMessages = new ArrayList<String>();
		
		try
		{
			id = Integer.parseInt(request.getParameter("id").trim());
		}
		catch(Exception ex)
		{
			validationMessages.add("<font size='4' color='red' > Wrong ID </font> <br />");
		}
		
		String model = request.getParameter("model").trim();
		String company = request.getParameter("company").trim();
		String camera = request.getParameter("camera").trim();
		int price = 0, updatedRows;
		PrintWriter out = response.getWriter();
		
		try
		{
			price = Integer.parseInt(request.getParameter("price").trim());
		}
		catch(Exception ex)
		{
			if (!request.getParameter("price").trim().equals(""))
			{
				validationMessages.add("<font size='4' color='red' > Wrong Price </font> <br />");
			}
			else
				price = 0;
		}
		
		if (!validationMessages.isEmpty())
		{
			request.setAttribute("validationMessages",validationMessages);
			request.getRequestDispatcher("displayError.jsp").forward(request,response);
		}
		else
		{
			MobileDAO mobileDAO = new MobileDAO();
			
			Mobile storedMobile = mobileDAO.get(id);
			
			if (model.equals("") || !model.matches("[a-zA-Z0-9 ]+"))
				model = storedMobile.getModel();
			if (company.equals("") || !company.matches("[a-zA-Z0-9 ]+"))
				company = storedMobile.getCompany();
			if (camera.equals("") || !camera.matches("[a-zA-Z0-9 ]+"))
				camera = storedMobile.getCamera();
			if (price == 0)
				price = storedMobile.getPrice();
		
			Mobile mobile = new Mobile(id, model, company, camera, price);
			
			updatedRows = mobileDAO.update(mobile);
			
			if (updatedRows > 0)
			{
				out.print("<font size='4' color='green'> Mobile Updated Successfully </font>");
			}
			else
			{
				out.print("<font size='4' color='red'> 0 Rows Updated</font>");
			}
		}
		out.print("<br />  <a href='javascript:history.back()'> Back </a>");
	}

}
